package marvin.webapp.audio.action;

import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import marvin.webapp.audio.displaybeans.ConfigNodeDisplayBean;
import marvin.webapp.audio.util.ConfigReader;

@ManagedBean
@SessionScoped
public class MenuAction extends AbstractAudioWebAction {

	private static final long serialVersionUID = 7015153638601208133L;
	
	private boolean configurationVisible;
	private List<ConfigNodeDisplayBean> files;
	private List<ConfigNodeDisplayBean> radios;
	
	@PostConstruct
	public void load() {
	}
	
	public void reloadTree(){
		try {
			treeBean.reloadTree();
		} catch (URISyntaxException e) {
			displayMessage(FacesMessage.SEVERITY_ERROR, "Tree reloading", e.getMessage());
		}
	}
	
	public void showTreeConfiguration(){
		List<String> rootFiles = ConfigReader.getParameters(ConfigReader.ROOT_FILES_KEY);
		
		// Files
		files = new LinkedList<ConfigNodeDisplayBean>();
		for(String path : rootFiles){
			String []values = path.split("=");
			String name = values[0];
			String url = "";
			if(values.length > 1){
				for(int i =1; i<values.length; i++)
					url+=values[i];
			}
			files.add(new ConfigNodeDisplayBean(name, url));
		}
		
		// Web radios
		radios = new LinkedList<ConfigNodeDisplayBean>();
		List<String> webRadios =ConfigReader.getParameters(ConfigReader.WEB_RADIOS_KEY);
		for(String urlValue: webRadios){
			String []values = urlValue.split("=");
			String name = values[0];
			String url = "";
			if(values.length > 1){
				for(int i =1; i<values.length; i++)
					url+=values[i];
			}
			radios.add(new ConfigNodeDisplayBean(name, url));
		}
		
		setConfigurationVisible(true);
	}

	public void saveConfiguration(){
		List<String> webRadios = new LinkedList<String>();
		List<String> filesValue = new LinkedList<String>();
		
		if(radios != null){
			for (ConfigNodeDisplayBean db : radios) {
				webRadios.add(db.getLabel()+"="+db.getValue());
			}
		}
		
		if(files != null){
			for (ConfigNodeDisplayBean db : files) {
				filesValue.add(db.getLabel()+"="+db.getValue());
			}
		}
		
		ConfigReader.setParameters(ConfigReader.WEB_RADIOS_KEY, webRadios);
		ConfigReader.setParameters(ConfigReader.ROOT_FILES_KEY, filesValue);
		
		boolean saved = ConfigReader.saveConfiguration(ConfigReader.CONFIG_FILE_ABSOLUTE_PATH);
		if(saved){
			displayMessage(FacesMessage.SEVERITY_INFO, "Configuration update", "Update done");
			setConfigurationVisible(false);
		} else {
			displayMessage(FacesMessage.SEVERITY_ERROR, "Configuration update", "Update not done");
		}
	}
	
	public void cancelConfiguration(){
		setConfigurationVisible(false);
	}
	
	public void setConfigurationVisible(boolean configurationVisible) {
		this.configurationVisible = configurationVisible;
	}

	public boolean isConfigurationVisible() {
		return configurationVisible;
	}

	public List<ConfigNodeDisplayBean> getFiles() {
		return files;
	}

	public List<ConfigNodeDisplayBean> getRadios() {
		return radios;
	}
}
